<!--  -->
<template>
  <div class=" flex-sub flex-column w-100P h-100P">
    <!-- table -->
    <div class="flex-sub">
      <el-table
        ref="table"
        v-loading="loading"
        :data="tableData"
        height="98%"
        :cell-style="{ background: '#fff' }"
        header-cell-class-name="table-header-cell"
        header-row-class-name="table-header-row"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>

        <el-table-column prop="fileId" label="车牌照">
          <template v-slot="{ row }">
            <img v-if="row.fileId" class="avatar-img" :src="row.fileId | imageFormat" alt="" />
            <img v-else src="../img/car-default.png" class="avatar-img" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="carNo" label="车牌号" />
        <el-table-column prop="personName" label="车主姓名" />

        <el-table-column fixed="right" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <div class="flex-row-center">
              <div class="btn-edit" @click="handleClick(scope.row)">修改</div>
              <div class="pd-lr-10 fz-14" style="color: #999;">|</div>
              <div class="delete-text" @click="handleDelete(scope.row.id)">删除</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <el-pagination
      hide-on-single-page
      background
      layout="prev, pager, next, total, jumper"
      :total="total"
      :page-size="queryInfo.pageSize"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script>
import { apiGetCarPageList, apiDeleteCarInfo, apiMultipeDeleteCarInfo } from '../api'
import { handleImage } from '@/utils/tools'

export default {
  props: {
    keyword: { type: String, default: '' },
    identityEnum: { type: String, default: '' },
  },
  filters: {
    imageFormat(value) {
      return handleImage(value)
    },
  },
  data() {
    return {
      queryInfo: {
        pageSize: 14,
        pageNo: 1,
        keyword: '',
        recordsCarVo: {
          identityEnum: null,
        },
      },
      total: 0,
      tableData: [],
      loading: false,
      selectList: [],
    }
  },

  watch: {
    keyword: {
      immediate: true,
      handler(newValue) {
        this.queryInfo.keyword = newValue
      },
    },
    identityEnum: {
      immediate: true,
      handler(newValue) {
        this.queryInfo.recordsCarVo.identityEnum = newValue
      },
    },
  },

  components: {},

  computed: {},

  mounted() {
    this.getTableData()
  },

  methods: {
    async getTableData() {
      // TODO 查询
      try {
        const res = await apiGetCarPageList(this.queryInfo)
        this.total = res.total || 0
        this.tableData = res.records || []
      } catch (error) {
        console.log(`apiGetPersonList request error`, error)
      }
    },
    handlePageChange(pageNo) {
      this.queryInfo.pageNo = pageNo
      this.getTableData()
    },
    handleDelete(id) {
      this.$confirm('您确定要删除本条记录吗？', '提示', { type: 'warning' }).then(async () => {
        await apiDeleteCarInfo(id)
        this.$message.success('移除成功')
        this.getTableData()
      })
    },
    handleClick(item) {
      this.$emit('edit', item)
    },
    handleSelectionChange(selectList) {
      this.selectList = selectList
    },
    async multipleDelete() {
      //  批量删除
      const cardIds = this.selectList.map(item => item.id)
      try {
        await apiMultipeDeleteCarInfo(cardIds)
        this.$message.success('批量删除成功！')
        this.getTableData()
      } catch (error) {
        console.log(`apiMultipeDeleteCarInfo error ${error}`)
      }
    },
  },
}
</script>
<style lang='scss' scoped>
.btn-edit {
  color: #0084ee;

  &:hover {
    cursor: pointer;
    opacity: 0.7;
  }
}

.avatar-img {
  width: vw(80px);
  height: auto;
}

.enter-text {
  color: #67c337;
}

.delete-text {
  color: #d12d1f;
}
</style>
